package utils;

import org.apache.commons.lang3.StringUtils;
import pojo.Context;
import server.Servlet;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class JarUtil {

    public static void getJarClazzName(String jarFile, String jarName, Context context) throws Exception {

        try {
            //通过将给定路径名字符串转换为抽象路径名来创建一个新File实例
            File f = new File(jarFile);
            URL url1 = f.toURI().toURL();
            URLClassLoader myClassLoader = new URLClassLoader(new URL[]{url1}, Thread.currentThread().getContextClassLoader());

            //通过jarFile和JarEntry得到所有的类
            JarFile jar = new JarFile(jarFile);
            //返回zip文件条目的枚举
            Enumeration<JarEntry> enumFiles = jar.entries();

            //测试此枚举是否包含更多的元素
            while (enumFiles.hasMoreElements()) {
                JarEntry entry = enumFiles.nextElement();
                if (entry.getName().indexOf("META-INF") < 0) {
                    String classFullName = entry.getName();
                    if (!classFullName.endsWith(".class")) {
                        classFullName = classFullName.substring(0, classFullName.length() - 1);
                    } else {
                        //去掉后缀.class
                        String className = classFullName.substring(0, classFullName.length() - 6).replace("/", ".");
                        Class<?> myclass = myClassLoader.loadClass(className);
                        //打印类名
                        System.out.println("*****************************");
                        System.out.println("全类名:" + className);
                        Object instance = myclass.newInstance();

                        if (instance instanceof Servlet) {
                            context.setUri("/" + StringUtils.substringBefore(jarName, "-") + "/" + StringUtils.substringAfterLast(className, "."));
                            context.setObject(instance);
                        }

                        //得到类中包含的属性
                        Method[] methods = myclass.getMethods();
                        for (Method method : methods) {
                            String methodName = method.getName();
                            System.out.println("方法名称:" + methodName);
                            Class<?>[] parameterTypes = method.getParameterTypes();
                            for (Class<?> clz : parameterTypes) {
                                // String parameterName = clas.getName();
                                String parameterName = clz.getSimpleName();
                                System.out.println("参数类型:" + parameterName);
                            }
                            System.out.println("==========================");
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static List<Object> getJarFileClassInstance(String jarFile, List<String> classNames) {
        List<Object> instanceList = new ArrayList<>();
        try {
            File f = new File(jarFile);
            URL url1 = f.toURI().toURL();
            URLClassLoader myClassLoader = new URLClassLoader(new URL[]{url1}, Thread.currentThread().getContextClassLoader());

            for (String className : classNames) {
                Class<?> clazz = myClassLoader.loadClass(className);
                Object instance = clazz.newInstance();
                instanceList.add(instance);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        return instanceList;
    }

    public static List<Context> getJarFile(String filePath, String host) throws Exception {
        // 获取jar文件名字
        List<Context> listJarFile = new ArrayList<>();

        File file = new File(filePath);
        String[] fileNames = file.list();
        for (String fileName : fileNames) {
            if (isJarFile(fileName)) {
                Context context = new Context();
                context.setHost(host);
                getJarClazzName(filePath + fileName, fileName, context);

                listJarFile.add(context);


            }
        }

        return listJarFile;
    }

    public static boolean isJarFile(String fileName) {
        // 判断是否是jar文件
        return fileName.endsWith(".jar");
    }


    public static void main(String[] args) throws Exception {

        List<Context> listJar = getJarFile("/Users/jason/webapp/", "localhost");

        System.out.println(listJar);

    }

}